#include "element/box.h"
#include <qgl.h>

double fBoxColor[BOX_POS_END][4] = {
    {1.0, 1.0, 1.0, 1.0},
    {0.4, 0.4, 0.4, 1.0},
    {0.4, 0.4, 0.4, 1.0},
    {0.4, 0.4, 0.4, 1.0},
    {1.0, 1.0, 1.0, 1.0},
    {1.0, 1.0, 0.1, 0.5}
};

void drawBoxBoundary(Box& box)
{
    glColor3d(0.7, 0.7, 0.7);
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y1, box.z0);
    glVertex3d(box.x0, box.y1, box.z0);
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x0, box.y0, box.z1);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x0, box.y0, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z0);
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x1, box.y1, box.z0);
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x0, box.y0, box.z1);
    glEnd();
    glBegin(GL_LINE_LOOP);
    glVertex3d(box.x0, box.y1, box.z0);
    glVertex3d(box.x1, box.y1, box.z0);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glEnd();
}

void drawBox(Box& box)
{
    glLineWidth(2.0);

    if (box.getStatus() == BOX_OUT)
    {
        // drawBoxBoundary(box);
        return;
    }

    glColor4dv(fBoxColor[box.getStatus()]);
    glBegin(GL_POLYGON);
    glNormal3d(0, 0, -1);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y1, box.z0);
    glVertex3d(box.x0, box.y1, box.z0);
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3d(0, 0, 1);
    glVertex3d(box.x0, box.y0, box.z1);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3d(-1, 0, 0);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x0, box.y0, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z0);
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3d(1, 0, 0);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x1, box.y1, box.z0);
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3d(0, -1, 0);
    glVertex3d(box.x0, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z0);
    glVertex3d(box.x1, box.y0, box.z1);
    glVertex3d(box.x0, box.y0, box.z1);
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3d(0, 1, 0);
    glVertex3d(box.x0, box.y1, box.z0);
    glVertex3d(box.x1, box.y1, box.z0);
    glVertex3d(box.x1, box.y1, box.z1);
    glVertex3d(box.x0, box.y1, box.z1);
    glEnd();
}
